<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "edu_menu".
 *
 * @property int $id
 * @property string $title 菜单名称
 * @property string $icon 菜单图标
 * @property string $select_icon 菜单图标
 * @property int $sort 排序
 * @property int $style 1底部菜单2上面菜单
 * @property string $url 菜单的url
 * @property string $des 菜单注释说明
 * @property string $status 状态
 */
class EduMenu extends \yii\db\ActiveRecord
{

    //定义场景
    const SCENARIO_UPDATE = 'update';
    const SCENARIO_UPLOAD = 'upload';
    const SCENARIO_UPLOAD_POST = 'upload-post';
    const SCENARIO_CREATE = 'create';

    //定义只有两种状态的枚举
    const STATUS_DISABLE = 0; //禁止
    const STATUS_ENABLE = 1; //启用



    public $file;

    public $files;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'edu_menu';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['title'], 'required'],
            [['sort', 'style','status'], 'integer'],
            [['sort'], 'number'],
            [['file', 'files'], 'file'],
            [['file', 'files'], 'safe'],
            [['title'], 'string', 'max' => 20],
            [['icon', 'select_icon'], 'string', 'max' => 500],
            [['url', 'des'], 'string', 'max' => 100],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '标题',
            'icon' => '图标|广告',
            'select_icon' => '选中图标|广告',
            'sort' => '排序',
            'style' => '菜单位置|类型',
            'url' => '链接',
            'des' => '描述',
            'status' => '状态',
            'file' => ' ',
        ];
    }

    /**
     * @return array
     * 定义枚举
     */
    public static function attributeValue()
    {
        return [
            'style' => [
                '1' => '底部菜单',
                '2' => '中间菜单',
                '3' => '首页广告',
                '4' => '门店端底部',
                '5' =>  '商城底部',
            ],
            'status' => [
                '0' => '禁用',
                '1' => '启动',
            ],
        ];
    }

    /**
     * @return string
     * 获取菜单
     */
    public function getStyleValue()
    {
    //return self::attributeValue()['style'][$this->style];
        switch ($this->style)
        {
            case 1:
                $value = '底部菜单';
                break;
            case 2:
                $value = '中间菜单';
                break;
            case 3:
                $value = '首页广告';
                break;
            case 4:
                $value = '门店端底部';
                break;
            case 5:
                $value = '商城底部';
                break;
            default:
                $value = '空';
                break;
        }
        return $value;
    }


    /**
     * @return string
     * 获取status状态
     */
    public function getStatusValue()
    {
        return $this->status == self::STATUS_ENABLE ? '启动' : '禁用';
    }

    /**
     * @return array
     * 获取状态
     */
    public static function allStatus()
    {
        return [self::STATUS_ENABLE=>'启用',self::STATUS_DISABLE=>'禁用'];
    }

    /**
     * @param $column
     * @param null $value
     * @return bool|mixed
     * 转化到gridview搜索
     */
    public static function dropDown($column, $value = null)
    {
        $dropDownList = [
            'isEnable' => [
                '0' => '禁用',
                '1' => '启用',
            ],
        ];

        if (null !== $value) {
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column][$value] : false;
        } else {
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column] : false;
        }
    }

    /**
     * @return array
     * 定义场景
     */
    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios[self::SCENARIO_UPDATE] = ['title', 'icon', 'select_icon', 'style', 'url', 'sort', 'des', 'status'];
        $scenarios[self::SCENARIO_UPLOAD] = ['file'];
        $scenarios[self::SCENARIO_UPLOAD_POST] = ['files'];
        $scenarios[self::SCENARIO_CREATE] = ['title', 'icon', 'select_icon', 'style', 'url', 'sort', 'des', 'status'];
        return $scenarios;
    }
}
